package com.lee.dutylower.utils;


import com.lee.dutylower.MyApplication;
import com.lee.dutylower.db.greenEntity.Sentry;
import com.lee.dutylower.db.greenEntity.Soldier;
import com.lee.dutylower.db.greenEntity.SoldierSentry;
import com.lee.dutylower.db.greenEntity.Squad;
import com.lee.dutylower.db.greenEntity.Times;
import com.lee.dutylower.db.greendao.DaoSession;
import com.lee.dutylower.db.greendao.SentryDao;
import com.lee.dutylower.db.greendao.SoldierDao;
import com.lee.dutylower.db.greendao.SoldierSentryDao;
import com.lee.dutylower.db.greendao.SquadDao;
import com.lee.dutylower.db.greendao.TimesDao;
import com.lee.dutylower.domain.SoldierCharacter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lijie on 2017/6/27.
 */

public class GreenDaoUtils {
    private GreenDaoUtils() {
    }

    public static void initDB(){
        DaoSession session = MyApplication.getApplication().getDaoSession();
        SquadDao squadDao = session.getSquadDao();
        SentryDao sentryDao = session.getSentryDao();
        SoldierSentryDao soldierSentryDao = MyApplication.getApplication().getDaoSession().getSoldierSentryDao();
        TimesDao timesDao = session.getTimesDao();
        SoldierDao soldierDao = session.getSoldierDao();

        List<Times> timesList = new ArrayList<>();
        timesList.add(new Times(1L, "07:30", "09:00"));
        timesList.add(new Times(2L, "09:00", "10:30"));
        timesList.add(new Times(3L, "10:30", "12:00"));
        timesList.add(new Times(4L, "12:00", "13:30"));
        timesList.add(new Times(5L, "13:30", "15:00"));
        timesList.add(new Times(6L, "15:00", "16:30"));
        timesList.add(new Times(7L, "16:30", "18:30"));
        timesList.add(new Times(8L, "18:30", "20:00"));
        timesList.add(new Times(9L, "20:00", "22:00"));
        timesList.add(new Times(10L, "22:00", "24:00"));
        timesList.add(new Times(11L, "24:00", "01:30"));
        timesList.add(new Times(12L, "01:30", "03:00"));
        timesList.add(new Times(13L, "03:00", "04:30"));
        timesList.add(new Times(14L, "04:30", "06:00"));
        timesList.add(new Times(15L, "06:00", "07:30"));
        timesDao.insertInTx(timesList);

        List<Sentry> sentryList = new ArrayList<>();
        sentryList.add(new Sentry(1L, "监护501", 45));
        sentryList.add(new Sentry(2L, "监护503", 15));
        sentryList.add(new Sentry(3L, "监护504", 45));
        sentryList.add(new Sentry(4L, "监护505", 45));
        sentryList.add(new Sentry(5L, "监护507", 45));
        sentryList.add(new Sentry(6L, "监护513", 45));
        sentryList.add(new Sentry(7L, "监护601", 45));
        sentryList.add(new Sentry(8L, "监护603", 15));
        sentryList.add(new Sentry(9L, "监护604", 45));
        sentryList.add(new Sentry(10L, "监护605", 45));
        sentryList.add(new Sentry(11L, "监护607", 45));
        sentryList.add(new Sentry(12L, "监护613", 45));
        sentryList.add(new Sentry(13L, "302", 90));
        sentryList.add(new Sentry(14L, "303", 90));
        sentryDao.insertInTx(sentryList);

        List<Squad> squadList = new ArrayList<>();
        squadList.add(new Squad(1L,"CLASS::A"));
        squadList.add(new Squad(2L,"CLASS::B"));
        squadList.add(new Squad(3L,"CLASS::C"));
        squadList.add(new Squad(4L,"CLASS::D"));
        squadList.add(new Squad(5L,"CLASS::E"));
        squadDao.insertInTx(squadList);

        List<Soldier> soldierList = new ArrayList<>();
        initSoldier(soldierList,1L,"A");
        initSoldier(soldierList,2L,"B");
        initSoldier(soldierList,3L,"C");
        initSoldier(soldierList,4L,"D");
        initSoldier(soldierList,5L,"E");
        soldierDao.insertInTx(soldierList);

        List<SoldierSentry> newsss = new ArrayList<>();
        List<SoldierSentry> oldsss = new ArrayList<>();
        Squad squad = squadDao.queryBuilder().where(SquadDao.Properties.SuqadId.eq(1L)).build().unique();
        initSS(newsss,oldsss,squad);
        squad = squadDao.queryBuilder().where(SquadDao.Properties.SuqadId.eq(2L)).build().unique();
        initSS(newsss,oldsss,squad);
        squad = squadDao.queryBuilder().where(SquadDao.Properties.SuqadId.eq(3L)).build().unique();
        initSS(newsss,oldsss,squad);
        squad = squadDao.queryBuilder().where(SquadDao.Properties.SuqadId.eq(4L)).build().unique();
        initSS(newsss,oldsss,squad);
        squad = squadDao.queryBuilder().where(SquadDao.Properties.SuqadId.eq(5L)).build().unique();
        initSS(newsss,oldsss,squad);

        soldierSentryDao.insertInTx(newsss);
        soldierSentryDao.insertInTx(oldsss);
    }

    private static void initSoldier(List<Soldier> soldierList, long squadId, String classname){
        for (int i = 1; i <= 12; i++) {
            Soldier s = new Soldier(null,squadId,classname+i,0,0,0);
            if (i == 1){
                s.setGrade(SoldierCharacter.MONITOR.getCode());
            }
            if (i == 2){
                s.setGrade(SoldierCharacter.MONITOR.getCode());
            }
            soldierList.add(s);
        }
    }

    private static void initSS(List<SoldierSentry> newsss, List<SoldierSentry> oldsss, Squad squad){
        List<Soldier> ss = squad.getSoldiers();
        Soldier s;
        for (int i = 0; i < ss.size(); i++) {
            s = ss.get(i);
            if (i <= 6){
                if (i == 0 || i== 1){
                    newsss.add(new SoldierSentry(null,s.getSID(),10L));
                    newsss.add(new SoldierSentry(null,s.getSID(),11L));
                }else {
                    newsss.add(new SoldierSentry(null,s.getSID(),1L));
                    newsss.add(new SoldierSentry(null,s.getSID(),2L));
                    newsss.add(new SoldierSentry(null,s.getSID(),3L));
                    newsss.add(new SoldierSentry(null,s.getSID(),4L));
                    newsss.add(new SoldierSentry(null,s.getSID(),5L));
                    newsss.add(new SoldierSentry(null,s.getSID(),6L));
                    newsss.add(new SoldierSentry(null,s.getSID(),7L));
                    newsss.add(new SoldierSentry(null,s.getSID(),8L));
                    newsss.add(new SoldierSentry(null,s.getSID(),9L));
                    newsss.add(new SoldierSentry(null,s.getSID(),12L));
                    newsss.add(new SoldierSentry(null,s.getSID(),13L));
                    newsss.add(new SoldierSentry(null,s.getSID(),14L));
                }

            }else if (i < 11){
                oldsss.add(new SoldierSentry(null,s.getSID(),2L));
                oldsss.add(new SoldierSentry(null,s.getSID(),3L));
                oldsss.add(new SoldierSentry(null,s.getSID(),4L));
                oldsss.add(new SoldierSentry(null,s.getSID(),5L));
                oldsss.add(new SoldierSentry(null,s.getSID(),7L));
                oldsss.add(new SoldierSentry(null,s.getSID(),12L));
                oldsss.add(new SoldierSentry(null,s.getSID(),13L));
                oldsss.add(new SoldierSentry(null,s.getSID(),14L));
            }else {
                oldsss.add(new SoldierSentry(null,s.getSID(),4l));
                oldsss.add(new SoldierSentry(null,s.getSID(),13L));
                oldsss.add(new SoldierSentry(null,s.getSID(),14L));
            }
        }

    }
}
